<?php

namespace app\store\model\presalesCustomer;

class PresalesCustomer extends \app\common\model\presalesCustomer\PresalesCustomer
{
    /**
     * 检索查询条件
     * @param array $param
     * @return array
     */
    private function getFilter(array $param = []): array
    {
        // 默认查询条件
        $param = $this->setQueryDefaultValue($param, [
            'search' => '',
            'pcs_id' => '',
            'pct_id' => '',
            'store_user_id' => '',
            'create_time' => [],
            'next_log_time' => [],
            'last_log_time' => [],
            'log_status' => -1
        ]);
        // 检索查询条件
        $filter = [];
        !empty($param['search']) && $filter[] = ['pc_id|full_name|idnumber|corporate_name|signatory|mobile|region|other_contacts|other_contacts_mobile', 'like', "%{$param['search']}%"];
        !empty($param['pcs_id']) && $filter[] = ['pcs_id', '=', $param['pcs_id']];
        !empty($param['pct_id']) && $filter[] = ['pct_id', '=', $param['pct_id']];
        !empty($param['store_user_id']) && $filter[] = ['store_user_id', '=', $param['store_user_id']];

        if (!empty($param['create_time'])) {
            $times = between_time($param['create_time']);
            $filter[] = ['create_time', '>=', $times['start_time']];
            $filter[] = ['create_time', '<', $times['end_time'] + 86400];
        }

        if (!empty($param['next_log_time'])) {
            $times = between_time($param['next_log_time']);
            $filter[] = ['next_log_time', '>=', $times['start_time']];
            $filter[] = ['next_log_time', '<', $times['end_time'] + 86400];
        }

        if (!empty($param['last_log_time'])) {
            $times = between_time($param['last_log_time']);
            $filter[] = ['last_log_time', '>=', $times['start_time']];
            $filter[] = ['last_log_time', '<', $times['end_time'] + 86400];
        }

        if ($param['log_status'] == 1) {
            // 当前无需跟进的类型id
            $pct_id = (new Type())->where(['not_log' => 1, 'is_delete' => 0])->column('pct_id');
            $filter[] = ['pct_id', 'not in', $pct_id];
            $filter[] = ['next_log_time', '>', time()];
            $filter[] = ['next_log_time', '<', strtotime(date("Y-m-d 23:59:59"))];
        } elseif ($param['log_status'] == 2) {
            // 当前无需跟进的类型id
            $pct_id = (new Type())->where(['not_log' => 1, 'is_delete' => 0])->column('pct_id');
            $filter[] = ['pct_id', 'not in', $pct_id];
            $filter[] = ['next_log_time', '<', time()];
            $filter[] = ['next_log_time', '<>', 0];
        }

        return $filter;
    }

    /**
     * 获取列表记录
     * @param array $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param = [], array $store = []): \think\Paginator
    {
        // 检索查询条件
        $filter = $this->getFilter($param);

        if ($store['user']['is_super'] != 1) {
            $filter[] = ['store_user_id', '=', $store['user']['store_user_id']];
        }
        // 查询列表数据
        return $this->where($filter)
            ->with(['store_user', 'source', 'type'])
            ->where('is_delete', '=', 0)
            ->order(['create_time' => 'desc'])
            ->paginate(!empty($param['pageSize']) ? $param['pageSize'] : 15);
    }

    public function byMobile($mobile)
    {
        return $this->where(['mobile' => $mobile, 'is_delete' => 0])->find();
    }

    /**
     * 添加新记录
     * @param array $data
     * @return bool|false
     */
    public function adds(array $data, array $store = []): bool
    {
        if (empty($data['pct_id'])) {
            //使用默认类型
            $default_type = (new \app\store\model\presalesCustomer\Type())->getDefaultType();
            if ($default_type) {
                $data['pct_id'] = $default_type->pct_id;
            }
        }

        //手机号码排重
        if (!empty($data['mobile']) && $this->byMobile($data['mobile'])) {
            $this->error = '操作失败：手机号码已存在！';
            return false;
        }


        $data['store_id'] = self::$storeId;
        $data['store_user_id'] = $store['user']['store_user_id'];
        return $this->save($data) !== false;
    }

    /**
     * 更新记录
     * @param array $data
     * @return bool
     */
    public function edits(array $data): bool
    {
        if (!empty($data['mobile']) && $this->mobile != $data['mobile'] && $this->byMobile($data['mobile'])) {
            $this->error = '操作失败：手机号码已存在！';
            return false;
        }
        return $this->save($data) !== false;
    }

    /**
     * 批量更新记录
     * @param array $data
     * @return bool
     */
    public function batch_edit(array $pc_ids, array $data): bool
    {
        return $this->where('pc_id', 'in', $pc_ids)->save($data) !== false;
    }

    /**
     * 删除记录 (软删除)
     * @return bool
     */
    public function setDeleted(): bool
    {
        // 同时删除关联的跟进记录
        (new Log())->where(['pc_id' => $this->pc_id, 'is_delete' => 0])->save(['is_delete' => 1]);
        return $this->save(['is_delete' => 1]) !== false;
    }

}
